﻿<%@ WebHandler Language="C#" Class="uploadHandler" %>

using System;
using System.Web;
using System.Drawing;

public class uploadHandler : IHttpHandler {
    
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.Files.Count > 0)
        {
            HttpFileCollection SelectedFiles = context.Request.Files;
            for (int i = 0; i < SelectedFiles.Count; i++)
            {
                HttpPostedFile PostedFile = SelectedFiles[i];
                string FileName = context.Server.MapPath("~/Cuenta/ImagenPerfil/" + ControlSesion.VariableSesion.Id_User.ToString() + ".jpg");
                string FileNameCrop = context.Server.MapPath("~/Cuenta/ImagenPerfil/crops/" + ControlSesion.VariableSesion.Id_User.ToString() + ".jpg");
                PostedFile.SaveAs(FileName);
                resizeImage(300,300,FileName);
                PostedFile.SaveAs(FileNameCrop);
                resizeImage(300,300,FileNameCrop);
                
            }
        }
        else
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Please Select Files");
        }
        context.Response.ContentType = "text/plain";
        context.Response.Write("Files Uploaded Successfully!!");
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    public void resizeImage(int newWidth, int newHeight, string stPhotoPath)
    {
        Image imgPhoto = Image.FromFile(stPhotoPath);

        int sourceWidth = imgPhoto.Width;
        int sourceHeight = imgPhoto.Height;

        //Consider vertical pics
        if (sourceWidth < sourceHeight)
        {
            int buff = newWidth;

            newWidth = newHeight;
            newHeight = buff;
        }

        int sourceX = 0, sourceY = 0, destX = 0, destY = 0;
        float nPercent = 0, nPercentW = 0, nPercentH = 0;

        nPercentW = ((float)newWidth / (float)sourceWidth);
        nPercentH = ((float)newHeight / (float)sourceHeight);
        if (nPercentH < nPercentW)
        {
            nPercent = nPercentH;
            destX = System.Convert.ToInt16((newWidth -
                      (sourceWidth * nPercent)) / 2);
        }
        else
        {
            nPercent = nPercentW;
            destY = System.Convert.ToInt16((newHeight -
                      (sourceHeight * nPercent)) / 2);
        }

        int destWidth = (int)(sourceWidth * nPercent);
        int destHeight = (int)(sourceHeight * nPercent);


        Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
                      System.Drawing.Imaging.PixelFormat.Format24bppRgb);

        bmPhoto.SetResolution(imgPhoto.HorizontalResolution,
                     imgPhoto.VerticalResolution);

        Graphics grPhoto = Graphics.FromImage(bmPhoto);
        grPhoto.Clear(Color.Black);
        grPhoto.InterpolationMode =
            System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

        grPhoto.DrawImage(imgPhoto,
            new Rectangle(destX, destY, destWidth, destHeight),
            new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
            GraphicsUnit.Pixel);

        grPhoto.Dispose();
        imgPhoto.Dispose();
        bmPhoto.Save(stPhotoPath);
    }
    

}